<?php
/**
 * Created by PhpStorm.
 * User: cd
 * Date: 2017/8/30
 * Time: 20:12
 */

namespace Admin\Controller;


use Think\Controller;

class CommonController extends Controller
{
    const RET_SUCCESS = 0;
    const RET_FAILED = 1;

    /**
     * ajax返回
     * @param int $code 错误码
     * @param string $msg 错误信息
     * @param array $data
     * @param int $count
     */
    public function ajaxRet($code = self::RET_SUCCESS, $msg = "SUCCESS", $data = [], $count = 0)
    {
        $ret = ['code' => $code, 'msg' => $msg, 'data' => $data, 'count' => $count];

        $this->ajaxReturn($ret);
    }

    public function __construct()
    {
        parent::__construct();

        // 验证登录
        if (!$this->isLogin()) {
            $this->error('您还没有登录哦！', U('Login/login'));
        }


        //保存侧栏导航中的id到session

        $get = I('get.id');
        session('id', $get);


        // 添加商品选择分类 查询分类
        $vo = D('sort');
        $res = $vo->select();
        $this->assign('res', $res);
    }

    public function isLogin()
    {
        //判断login页面不用验证
        if (CONTROLLER_NAME == "Login") {
            return true;
        }
        //判断已经登录的页面不用验证
        if (session('user')) {
            return true;
        }

        return false;
    }
}